;---------------
; Melzars maze fighter
; before starting the script
; make sure you have maze key
; and it's visible

; generated by Kaitnieks from:
; Easy Auto-Fight
; Auto-Generated by AutoRune
; Created by Kaitnieks
; For fighting NPCs type 177,47
; With fight mode 1
; Don't attack if HP below 10


SetOnPlaceTime(600)
OnLogin(@Init)
@Init:
EnableEvents()
; Set up some events
OnStatsChanged(@StatsChange,%Stat)
OnServerMessage(@ServMes)
OnItemAppeared(@NewItem,%ItemID,%X,%Y)

; Set it to train strength or whatever
FightMode(1)
; Ignore those NPCs that are in fight
CheckFighters(1)
; Let AR know, that we are not in fight
SetVarNum(%FightOver,1)

GoToIfInInventory(@HasMazeKey,421)
MessageBox("You don't have maze key or it's invisible. Drop and pick it up again if you do have it")
@HasMazeKey:


@MainLoop:
;Check if we have misteriously disappeared from the maze
GoToIfCoordsIn(@InMaze,290,626,300,638)
Debug("We're not in the maze!!!!")
StopAndLogOut()
@InMaze:
;Check if we're in the small room behind door
GoToIfCoordsIn(@SmallRoom,300,638,297,637)
GoTo(@NotSmallRoom)
@SmallRoom:
Debug("We're stuck in the small room behind door")
;Try to get out
MoveTo(298,637)
Action(298,637)
OpenDoor(298,637,0)
Wait(20)
MoveTo(298,634)
@NotSmallRoom:

;Check if we have low HP and go to cook some meat
SetVarMyHP(%HP)
GoToIfVarBelowNum(@CookMeat,%HP,10)


; Don't attack if we are already fighting
GoToIfVarEqualNum(@DontAttack,%FightOver,0)


; Attack if any enemies visible
GoToIfNPCNearIn(@Attack,290,626,300,636,177,47)
; Otherwise don;t attack
GoTo(@DontAttack)


@Attack:
; Attack only if my HP is above certain number
; or unknown
SetVarMyHP(%HP)
GoToIfVarEqualNum(@Attack2,%HP,0)
GoToIfVarBelowNum(@DontAttack,%HP,10)


@Attack2:
; get coords and ID of nearest enemy
SetVarsNearestNPCIn(%x,%y,%ID,290,626,300,636,177,47)
; attack the enemy
ActionVarVar(%x,%y)
AttackNPCVar(%ID)
; let AR know that we are in fight now
SetVarNum(%FightOver,0)
; reset idle counter
SetVarNum(%IdleC,0)


@DontAttack:
Wait(5,6)

; count how many loops we are idling
AddVarNum(%IdleC,1)
; if more than 20 loops - attack next enemy
GoToIfVarAboveNum(@FightNext,%IdleC,20)
; if more than 4 loops - burry all bones we have
---GoToIfVarAboveNum(IHaveBones,%IdleC,4)
GoTo(@MainLoop)


@FightNext:
; tell AR that we are not fighting
SetVarNum(%FightOver,1)
; reset idle counter
SetVarNum(%IdleC,0)
GoTo(@MainLoop)


;--- OnStatChaged
@StatsChange:
@CanMove:
;If my HitPoints skill changed, it means
;fight is over
GoToIfVarEqualNum(@SetCanMove,%Stat,3)
ERet()
@SetCanMove:
SetVarNum(%FightOver,1)
SetVarNum(%IdleC,0)
ERet()


;--- OnServerMessage
@ServMes:
;-- If someone stole your enemy
GoToIfLastServerMessageIs(@RunSomewhere,"I can't get close enough")
;-- or your victim runs away
GoToIfLastServerMessageIs(@ResetGo,"Your opponent is retreating")
ERet()
;-- then let AR know that fight is over
@RunSomewhere:
---Debug("Testing random running away")
SetVarMyX(%X)
SetVarMyY(%Y)
---DebugVar(%X,%Y)
SetVarRandom(%DX,5)
AddVarNum(%DX,-2)
SetVarRandom(%DY,5)
AddVarNum(%DY,-2)
AddVar(%X,%DX)
AddVar(%Y,%DY)
ActionVarVar(%X,%Y)
---DebugVar(%X,%Y)
@ResetGo:
SetVarNum(%FightOver,1)
ERet()


@CookMeat:
SetSkipDestMode(0)
@RetryLeaveMaze:
MoveTo(290,632)
Action(290,632)
WithItemAtDoor(290,632,1,421)
Wait(40)
GoToIfCoordsIn(@OutSideMaze,289,632,289,632)
GoTo(@RetryLeaveMaze)
@OutSideMaze:
;Walk to the house with range
MoveTo(286,632,-8,8)
MoveTo(278,652,-1,1)
MoveTo(277,657,-5,5)
Action(272,662)
OpenDoor(272,662,1)
MoveTo(268,662,-1,1)
;Cook and eat all meat
@CookLoop:
Action(267,663)
AtObjectWithItemByID(266,663,503)
---Cook(267,663,266,663,503)
Wait(30)
DropItemByID(134)
UseItemByID(132)
Wait(10)
GoToIfInInventory(@CookLoop,503)
MoveTo(270,663,1,-1)
MoveTo(272,662)
MoveTo(282,652,0,-4)
MoveTo(282,641,1,-1,1,-3,6,-8)
MoveTo(289,632)
@RetryEnterMaze:
Action(289,632)
WithItemAtDoor(290,632,1,421)
Wait(40)
GoToIfCoordsIn(@InSideMaze,290,632,290,632)
GoTo(@RetryEnterMaze)
@InSideMaze:
GoTo(@MainLoop)




;--- New item appeared
@NewItem:
;-- If we're not fighting or burying bones
; then take the item
GoToIfVarEqualNum(@TakeItems,%FightOver,1)
ERet()
@TakeItems:
SetVarMyX(%MX)
SetVarMyY(%MY)
;Only take the item if it's right under my feet
GoToIfVarNotEqual(@DontGrabAnything,%X,%MX)
GoToIfVarNotEqual(@DontGrabAnything,%Y,%MY)
;-- Only take certain items
GoToIfVarNotEqualNum(@DontGrabBones,%ItemID,20)
-GoTo(@GrabBones)
@DontGrabBones:
GoToIfVarNotEqualNum(@DontGrabCoins,%ItemID,10)
-GoTo(@GrabCoins)
@DontGrabCoins:
GoToIfVarNotEqualNum(@DontGrabMeat,%ItemID,503)
GoToIfBagFull(@DontGrabMeat)
GoTo(@GrabMeat)
@DontGrabMeat:
@DontGrabAnything:
ERet()



;---Pick up bones
@GrabBones:
ActionVarVar(%X,%Y)
TakeItemVarVar(%X,%Y,20)
Wait(10)
GoTo(@DontGrabBones)

;---Pick up coins
@GrabCoins:
ActionVarVar(%X,%Y)
TakeItemVarVar(%X,%Y,10)
Wait(10)
GoTo(@DontGrabCoins)

;---Pick up meat
@GrabMeat:
ActionVarVar(%X,%Y)
TakeItemVarVar(%X,%Y,503)
Wait(10)
GoTo(@DontGrabMeat)



